//
//  CustomViewController.m
//

#import "CustomViewController.h"
#import "CustomNavigationController.h"
#import "UIColor+Expanded.h"
#import "UIView+Frame.h"
#import "UIView+Corner.h"

@interface CustomViewController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation CustomViewController

- (instancetype)initWithCoder:(NSCoder *)coder{
    
    self = [super initWithCoder:coder];
    
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"";
    
    self.view.backgroundColor = [UIColor colorWithRGBHexString:@"141416"];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.custemNavgationHidden = NO;
    
    if (@available(iOS 11.0, *)){
        self.edgesForExtendedLayout = UIRectEdgeTop;
    }else{
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.navgationbackColor = [UIColor clearColor];
    
    [self.view addSubview:self.customNavagetionView];
}

- (UIView *)customNavagetionView{
    CGFloat statusHeight = CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + 44;
    if (@available(iOS 13.0, *)) {
       statusHeight = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;
    }

    if (!_customNavagetionView) {
        _customNavagetionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusHeight)];

        _customNavagetionView.tag = 9999;
        _customNavagetionView.backgroundColor =  [UIColor clearColor];
    }
    
    _customNavagetionView.y = (self.edgesForExtendedLayout == UIRectEdgeNone)? -statusHeight : 0;
    
    return _customNavagetionView;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    
    return UIStatusBarStyleLightContent;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if (self.custemNavgationHidden) {
        self.customNavagetionView.alpha =0;
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }else{
        self.customNavagetionView.alpha =1;
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
    
    if (self.presentingViewController&&!self.navigationController) {//当Controller了被直接present时添加
        
        UIButton * backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, self.customNavagetionView.height-40, 60, 35)];
        [backButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        [backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
        [backButton addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
        [self.customNavagetionView addSubview:backButton];
        
        if (self.title.length) {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,self.customNavagetionView.height-35, 200, 30)];
            titleLabel.centerX = self.customNavagetionView.centerX;
            titleLabel.text = self.customTitel;
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
            [self.customNavagetionView addSubview: titleLabel];
            titleLabel.textColor = [UIColor blackColor];
        }
    }
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    if ([self.navigationItem.titleView isKindOfClass:[UILabel class]]) {
        UILabel * customTitle = (UILabel *)self.navigationItem.titleView;
        customTitle.textColor = [UIColor whiteColor];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    
    [self.view bringSubviewToFront:self.customNavagetionView];
}

- (void)setCustomTitel:(NSString *)customTitel{
    
    _customTitel = customTitel;
    self.navigationItem.title = @"";
        
    UILabel * titleLabel = (UILabel *)self.navigationItem.titleView;
    
    if (_customTitel.length) {
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
        self.navigationItem.titleView = titleLabel;
    }
    
    titleLabel.text = _customTitel;
    [titleLabel sizeToFit];
    titleLabel.textColor = self.navigationController.navigationBar.tintColor;
}


- (BOOL)prefersStatusBarHidden{
    return NO;
}

- (void)setNavgationbackColor:(UIColor *)navgationbackColor{
    
    _navgationbackColor = navgationbackColor;
    
    UIView * view = [self.view viewWithTag:9999];
    view.backgroundColor = _navgationbackColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dismissViewController{
        
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
